import { reqGetChannels, reqGetAllChannels } from '@/api/channels'
const state = {
  channels: [],
  allChannels: []
}
const mutations = {
  getChannels(state, channels) {
    state.channels = channels
  },
  getAllChannels(state, allChannels) {
    state.allChannels = allChannels
  },
  addChannel(state, channel) {
    state.channels.push(channel)
  },
  delChannel(state, channel) {
    state.channels = state.channels.filter(item => item.id !== channel.id)
  }
}
const actions = {
  async getChannelsAsync(context) {
    const res = await reqGetChannels()
    context.commit('getChannels', res.data.channels)
  },
  async getAllChannelsAsync(context) {
    const res = await reqGetAllChannels()
    context.commit('getAllChannels', res.data.channels)
  }
}
const getters = {
  optionalChannels: (state) => {
    const res = state.allChannels.filter(item1 => {
      const index = state.channels.findIndex(item2 => item2.id === item1.id)
      return index === -1
    })
    return res
  }
}

export default {
  namespaced: true,
  state,
  actions,
  mutations,
  getters
}
